<template>
  <view class="container">
    <!-- 爱车列表 -->
    <view class="car-list">
      <view class="cell" v-for="(item, index) in carList" :key="index" @click="onClickItem(item)">
        <image class="logo" :src="item.brandImg"></image>
        <view class="info">
          <text class="title">{{item.carName}}</text>
          <text class="model">{{item.carNamePart}}</text>
        </view>
        <view class="radio" :class="{active: item.selected}">
          <u-icon v-show="item.selected" name="checkmark" color="#333" size="28"></u-icon>
        </view>
      </view>
    </view>
    
    <!-- 添加爱车 -->
    <view class="add-car" @click="onClickAddCar">
      <u-icon name="plus-circle" size="44" color="#333"></u-icon>
      <text class="text">添加爱车</text>
    </view>
    
    <!-- 底部按钮 -->
    <view class="footer">
      <view class="btn-box">
        <view class="btn flex flex-ai-c flex-jc-c" @click="onConfrim">
          <text class="text">确认</text>
        </view>
      </view>
    </view>
    
    <!-- 弹窗 -->
    <u-popup mode="center" v-model="show" border-radius="20">
      <view class="popup-content">
        <text class="title">温馨提示</text>
        <text class="content">该车辆暂无保养订单，确认继续添加？</text>
        <view class="btn-box">
          <view class="btn cancle" @click="show = false"><text>取消</text></view>
          <view class="btn confrim" @click="onClickNext"><text>继续</text></view>
        </view>
      </view>
    </u-popup>
    
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      carList: [], // 汽车列表
      current: null, // 当前选中项
      specifAll: {
        brandlist: null, //品牌
        Serieslist: null, //系列
        Displacement: null, //排量
        Modelyear: null, //年款
        Configulists: null, //车子
      }
    }
  },
  computed: {
    userinfo() {
      return this.$store.state.userinfo
    }
  },
  onLoad(option) {
    if(option.storeId) {
      this.storeId = option.storeId
      uni.showLoading({
        title: '加载中'
      })
      this.getMyLoveCar(option.storeId)
    }
    
    // 选择品牌，系列，排量，年款，车子
    uni.$on('changeVehicle', data => {
      Object.assign(this.specifAll, data)
      if (data.licenseplate && data.pageFrom === 'changeVehicle') {
        this.addCar(this.specifAll)
      }
    })
  },
  methods: {
    // 点击添加爱车
    onClickAddCar() {
      uni.navigateTo({
        url: '/pages_common/addYourCar/addYourCar?type=120&pageFrom=changeVehicle'
      })
    },
    async getUserInfo(callback) {
      if(this.userinfo) {
        callback && callback(true)
      } else {
        try{
          let res = await this.$api.user.GetUserInfomation()
          if(res.code === 200) {
            this.$store.commit('userinfo', res.data)
            callback && callback(true)
          } else {
            callback && callback(false)
          }
        }catch(e){
          callback && callback(false)
        }
      }
    },
    // 获取我的爱车
    getMyLoveCar(storeId) {
      this.getUserInfo(async data => {
        if(data) {
          try{
            let res = await this.$api.commodity.ClientQueueInfoBack({
              storeid: storeId
            })
            if(res.code === 200){
              if(res.data.cars.length > 0) {
                this.carList = res.data.cars.map(item => {
                  item.selected = false
                  return item
                })
              }
            }
            uni.hideLoading()
          }catch(e){
            this.loading = false
            uni.hideLoading()
          }
        }
      })
    },
    onClickItem(data) {
      this.carList.map(item => {
        item.selected = item.carId === data.carId
      })
      this.current = data
    },
    // 添加爱车
    async addCar(data) {
      try{
        uni.showLoading({
          title: '加载中'
        })
        let res = await this.$api.common.AddCar({
          uid: this.userinfo.accountid,
          brand_Id: data.brandlist.bid,
          brand_Name: data.brandlist.brand,
          car_Type_Id: data.Serieslist.carTypeId,
          car_Type_Name: data.Serieslist.carTypeName,
          car_Id: data.Configulist.id,
          car_Name: data.Configulist.name,
          displacement: data.Displacement.pName,
          yeartype: data.Configulist.date,
          car_Num: data.licenseplate, //车牌号
        })
        uni.hideLoading()
        this.getMyLoveCar(this.storeId)
      }catch(e){
        uni.hideLoading()
      }
    },
    // 点击确认
    onConfrim() {
      if(!this.current) return false
      if(this.current.orderCount < 1) {
        this.show = true
      } else {
        this.onClickNext()
      }
    },
    onClickNext() {
      uni.$emit('changeVehicle', this.current)
      uni.navigateBack({
        delta: 1
      })
    }
  },
  onUnload() {
    uni.$off('changeVehicle')
  }
}
</script>

<style lang="scss">
page{
  background-color: #f6f6f6;
}
.container{
  overflow: hidden;
  padding-bottom: 200rpx;
  padding-bottom: calc(constant(safe-area-inset-bottom) + 200rpx);
  padding-bottom: calc(env(safe-area-inset-bottom) + 200rpx);
  .car-list{
    .cell{
      background-color: #fff;
      box-shadow: 0 0 10rpx 0 rgba(0, 0, 0, 0.1);
      border-radius: 10rpx;
      margin: 24rpx;
      display: flex;
      align-items: center;
      padding: 40rpx 30rpx 40rpx 40rpx;
      .logo{
        width: 90rpx;
        height: 90rpx;
      }
      .info{
        flex: 1;
        margin: 0 36rpx;
        display: flex;
        flex-direction: column;
        justify-content: center;
        .title{
          font-size: 34rpx;
          color: #333;
          font-weight: bold;
          display: -webkit-box;
          overflow: hidden;
          white-space: normal !important;
          text-overflow: ellipsis;
          word-wrap: break-word;
          -webkit-line-clamp: 1;
          -webkit-box-orient: vertical;
        }
        .model{
          display: -webkit-box;
          overflow: hidden;
          white-space: normal !important;
          text-overflow: ellipsis;
          word-wrap: break-word;
          -webkit-line-clamp: 1;
          -webkit-box-orient: vertical;
          font-size: 28rpx;
          color: #666;
          margin-top: 10rpx;
        }
      }
      .radio{
        display: flex;
        align-items: center;
        justify-content: center;
        box-sizing: border-box;
        width: 48rpx;
        height: 48rpx;
        border-radius: 48rpx;
        border: 2rpx solid #d0d0d0;
        margin-right: 20rpx;
        &.active{
          background: linear-gradient(0deg, #FBD220, #FFE23E);
          border: none;
        }
      }
    }
  }
  .add-car{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 10rpx 0;
    margin: 24rpx;
    .text{
      margin-left: 14rpx;
      font-size: 30rpx;
      color: #333;
    }
  }
  .footer{
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding-bottom: 0;  
    padding-bottom: constant(safe-area-inset-bottom);  
    padding-bottom: env(safe-area-inset-bottom);
    .btn-box{
      padding: 0 24rpx 80rpx;
      .btn{
        height: 88rpx;
        background: linear-gradient(0deg, #FBD220, #FFE23E);
        border-radius: 44rpx;
        .text{
          font-size: 30rpx;
          color: #3d3d3d;
        }
      }
    }
  }
  .popup-content{
    width: 600rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 50rpx 40rpx 40rpx 40rpx;
    box-sizing: border-box;
    .title{
      font-size: 34rpx;
      color: #333;
      font-weight: bold;
    }
    .content{
      font-size: 28rpx;
      color: #333;
      margin: 30rpx 0 50rpx;
    }
    .btn-box{
      display: flex;
      width: 100%;
      .btn{
        flex: 1;
        height: 88rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28rpx;
      }
      .cancle{
        background-color: #333;
        color: #FFE23E;
        border-radius: 44rpx 0 0 44rpx;
        font-size: 28rpx;
      }
      .confrim{
        background: linear-gradient(0deg, #FBD220, #FFE23E);
        color: #333;
        border-radius: 0 44rpx 44rpx 0;
        font-size: 30rpx;
        margin-left: 2rpx;
      }
    }
  }
}
</style>
